<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<script type="text/javascript" src="${pageContext.request.contextPath }/statics/js/jquery-1.8.3.min.js"></script>
<%--
  Created by IntelliJ IDEA.
  User: hnjy
  Date: 2025/2/22
  Time: 13:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <style>


        img {
            width: 50px;
            height: 50px;
        }
        body {
            font-family: Arial, sans-serif;
        }

        .pet-info {
            margin-bottom: 20px;
        }

        .comment-input {
            margin-top: 20px;
        }

        .comment-input textarea {
            width: 100%;
            height: 100px;
        }

        .comment-item {
            border: 1px solid #ccc;
            padding: 10px;
            margin-bottom: 10px;
        }

        .comment-item img {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            vertical-align: middle;
        }

        .comment-item.info {
            display: inline-block;
            margin-left: 10px;
        }

        .comment-item.username {
            font-weight: bold;
        }

        .comment-item.time {
            color: #999;
        }

        .reply-btn {
            margin-top: 5px;
        }

        .reply-list {
            margin-left: 30px;
        }

        /* 弹窗遮罩样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0, 0, 0, 0.4);
        }

        /* 弹窗内容样式 */
        .modal-content {
            background-color: #fefefe;
            margin: 15% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
            max-width: 500px;
            border-radius: 5px;
            box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
        }

        /* 关闭按钮样式 */
        .close {
            float: right;
            font-size: 28px;
            font-weight: bold;
        }

        .close:hover,
        .close:focus {
            color: #000;
            text-decoration: none;
            cursor: pointer;
        }
        .btn{
            background-color: #337AB7;
            color: white;
            border-radius: 3px;
            border: unset;
        }
    </style>
</head>
<body>

<h5 style="text-align: center">
    ${forum.title}
</h5>
<div style="text-align: right">${forum.time}</div>
<div>${forum.content}</div>



<div id="comment-list">
    <!-- 遍历顶级评论 -->
    <c:forEach items="${list}" var="topComment">
        <div class="comment-item">
            <img src="${topComment.user.pic}" alt="头像">
            <div class="info">
                <span class="username">${topComment.user.username}</span>
                <span class="time">${topComment.commentTime}</span>
                <p>${topComment.content}</p>
            </div>
            <button class="btn" onclick="showDiv(${topComment.id})">回复</button>
            <!-- 遍历一级子评论 -->
            <c:forEach items="${topComment.forumCommentList}" var="firstLevelReply">
                <div class="comment-item" style="margin-left: 30px;">
                    <img src="${firstLevelReply.user.pic}" alt="头像">
                    <div class="info">
                        <span class="username">${firstLevelReply.user.username}</span>
                        <span class="time">${firstLevelReply.commentTime}</span>
                        <p>${firstLevelReply.content}</p>
                    </div>
                    <button class="btn" onclick="showDiv(${firstLevelReply.id})">回复</button>
                    <!-- 遍历二级子评论 -->
                    <c:forEach items="${firstLevelReply.forumCommentList}" var="secondLevelReply">
                        <div class="comment-item" style="margin-left: 60px;">
                            <img src="${secondLevelReply.user.pic}" alt="头像">
                            <div class="info">
                                <span class="username">${secondLevelReply.user.username}</span>
                                <span class="time">${secondLevelReply.commentTime}</span>
                                <p>${secondLevelReply.content}</p>
                            </div>
                        </div>
                    </c:forEach>
                </div>
            </c:forEach>
        </div>
    </c:forEach>
</div>

<div class="comment-input"  style="clear: both;margin-top: 20px">
    <form action="${pageContext.request.contextPath}/forumComment/add" method="post">
        <textarea name="content" placeholder="来说几句吧" id="content2"></textarea>

        <input type="hidden" name="forumId" value="${id}">
        <input type="button" value="评论" class="btn" onclick="add2()" >
    </form>
</div>




<div id="modal" class="modal">
    <div class="modal-content">
        <span class="close">&times;</span>
        <div class="comment-input"  >
            <form action="${pageContext.request.contextPath}/forumComment/add" method="post">

                <textarea name="content" placeholder="来说几句吧" id="content"></textarea>
                <input type="hidden" name="id" value="${id}"> <!-- 这里假设宠物ID为1，实际应动态获取 -->
                <input onclick="add()" type="button" value="回复" class="btn" style="background-color:#337AB7;color: white ">
            </form>
        </div>
    </div>
</div>
<input type="hidden" id="forumId" value="${id}">
<input type="hidden" id="parentId" >
<script>
    
    function add() {
        var content = $("#content").val();
        var parentId = $("#parentId").val();
        var forumId = $("#forumId").val();

        $.post("${pageContext.request.contextPath}/sys/forumComment/add", "content=" + content + "&parentId=" + parentId+"&forumId="+forumId, function (data) {
            if (data == true) {

                alert("发布成功");
                $("#content").val('')
                location = "${pageContext.request.contextPath}/sys/forum/detail?id="+forumId
            } else {
                alert("发布失败");
            }

        })
    }
    function add2() {
        var content = $("#content2").val();

        var forumId = $("#forumId").val();

        $.post("${pageContext.request.contextPath}/sys/forumComment/add", "content=" + content +"&forumId="+forumId, function (data) {
            if (data == true) {
                alert("发布成功");

                $("#content2").val('')
                location = "${pageContext.request.contextPath}/sys/forum/detail?id="+forumId
            } else {
                alert("发布失败");
            }

        })
    }
    function  showDiv(id) {
        // 获取按钮、弹窗和关闭按钮元素

        const modal = document.getElementById('modal');
        const closeBtn = document.querySelector('.close');
        document.getElementById("parentId").value = id;

        modal.style.display = 'block';


        // 点击关闭按钮关闭弹窗
        closeBtn.addEventListener('click', () => {
            modal.style.display = 'none';
        });

        // 点击弹窗外部关闭弹窗
        window.addEventListener('click', (event) => {
            if (event.target === modal) {
                modal.style.display = 'none';
            }
        });
    }


    // 模拟提交评论，实际需要通过AJAX与后端交互
    document.querySelector('.comment-box button').addEventListener('click', function () {
        const commentContent = document.querySelector('.comment-box textarea').value;
        if (commentContent.trim() === '') {
            alert('请输入评论内容');
            return;
        }
        console.log('提交的评论内容:', commentContent);
        // 清空输入框
        document.querySelector('.comment-box textarea').value = '';
    });
</script>
</body>
</html>
